﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Se7en.Admin.Common;
using Se7en.Admin.Model;
using Se7en.Admin.Business;
using ERMSys.Framework.Utilities;

namespace Se7en.Admin.Web.Controls
{
    public partial class RoleEdit : UserControlBase
    {
        #region Fields

        Role role = new Role();

        #endregion

        #region Properties

        public int RoleId
        {
            get
            {
                if (ViewState["RoleId"] == null)
                    return 0;
                return (int)ViewState["RoleId"];
            }

            set
            {
                ViewState["RoleId"] = value;
            }
        }

        public string OnCancelClientClick
        {
            get
            {
                return btnCancel.OnClientClick;
            }

            set
            {
                btnCancel.OnClientClick = value;
            }
        }

        #endregion

        #region GUI Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (!IsPostBack)
            {
                this.ValidateMaxLengthMultiline(txtDescr);
                BindRoleType();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ValidateData())
                return;
            if (Mode == Modes.Add)
            {
                this.GetData(role, false);
                DBContext.Role_AddNew(role);
            }
            else
            {
                role = DBContext.Role_GetRoleById(this.RoleId);
                if (role != null)
                {
                    this.GetData(role, true);
                    DBContext.Update(role);
                }
            }
            DBContext.SaveAllChanges();
            base.OnSuccess(sender, e);
        }

        #endregion

        #region Methods

        public void BindRoles(int roleId)
        {
            Role role = DBContext.Role_GetRoleById(roleId);
            if (role != null)
            {
                this.RoleId = roleId;
                ddlRoleType.SelectedValue = role.Type.ToString();
                txtRoleName.Text = role.Name;
                txtDescr.Text = role.Description;
            }
        }

        public void ClearData(string roleType)
        {
            this.RoleId = 0;
            ddlRoleType.SelectedValue = roleType;
            txtRoleName.Text = "";
            txtDescr.Text = "";
        }

        public bool ValidateData()
        {
            bool hasError = false;
            if (string.IsNullOrEmpty(txtRoleName.Text.Trim()))
            {
                lblError.Text += string.Format(MessageRes.Required + "<br/>", "Role Name");
                hasError = true;
            }
            else
            {
                if (this.RoleId == 0)
                {
                    if (DBContext.Role_CheckDuplicateRole(ddlRoleType.SelectedValue.ConvertToInt32(), txtRoleName.Text.Trim()))
                    {
                        lblError.Text += string.Format(MessageRes.Duplicated + "<br/>", "Role Name");
                        hasError = true;
                    }
                }
                else
                {
                    if (DBContext.Role_CheckExistedRole(this.RoleId, ddlRoleType.SelectedValue.ConvertToInt32(), txtRoleName.Text.Trim()))
                    {
                        lblError.Text += string.Format(MessageRes.Duplicated + "<br/>", "Role Name");
                        hasError = true;
                    }
                }
            }
            return hasError;
        }

        private void GetData(Role obj, bool isEdit)
        {
            obj.Name = txtRoleName.Text.Trim();
            obj.Description = txtDescr.Text.Trim();
            obj.Type = ddlRoleType.SelectedValue.ConvertToInt32();
        }

        private void BindRoleType()
        {
            ddlRoleType.DataSource = EnumHelper.GetEnumData(typeof(RoleType));
            ddlRoleType.DataTextField = "Text";
            ddlRoleType.DataValueField = "Value";
            ddlRoleType.DataBind();
        }


        #endregion
    }
}